package com.zxu.test.grav;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.Observable;
import androidx.lifecycle.ViewModelProviders;

import com.zxu.test.EventBusUtil.AppEventMsg;
import com.zxu.test.RxUtil.RxBus;
import com.zxu.test.RxUtil.RxEventMsg;
import com.zxu.test.activity.TryActivity;
import com.zxu.test.base.BaseFragment;
import com.zxu.test.header.CommonTipHeaderViewModel;
import com.zxu.test.studyapp.MainActivity;
import com.zxu.test.studyapp.databinding.ActivityGravBinding;
import com.zxu.test.util.AcUtils;
import com.zxu.test.util.Event;
import com.zxu.test.util.IntentMsg;
import com.zxu.test.view.ToastCustom;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class GravFragment extends BaseFragment {
    private CommonTipHeaderViewModel mHeader;
    private ActivityGravBinding mBinding;
    private GravViewModel mViewModel;

    public GravFragment() { }

    public static GravFragment getInstance() {
        return new GravFragment();
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = ActivityGravBinding.inflate(inflater, container, false);
        mViewModel = ViewModelProviders.of(getActivity()).get(GravViewModel.class);
        mBinding.setViewModel(mViewModel);
        mHeader = ViewModelProviders.of(this).get(CommonTipHeaderViewModel.class);
        mHeader.setTitle("Grav动画");
        mHeader.setTip("取消");
        mBinding.setHeader(mHeader);
        return  mBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setupListener();
    }

    private void setupListener() {
        mHeader.getBackCommand().observe(getViewLifecycleOwner(), this::close);
        mHeader.getTipEvent().observe(getViewLifecycleOwner(), this::historyList);
        mViewModel.btnEvent.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
            @Override
            public void onPropertyChanged(Observable sender, int propertyId) {
                IntentMsg intentMsg = new IntentMsg();
                intentMsg.Id ="1";
                AcUtils.launchActivity(getContext(), MainActivity.class, intentMsg);
            }
        });
        mViewModel.tryEvent.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
            @Override
            public void onPropertyChanged(Observable sender, int propertyId) {
                IntentMsg intentMsg = new IntentMsg();
                intentMsg.Id ="2";
                AcUtils.launchActivity(getContext(), TryActivity.class, intentMsg);
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    private void close(Event<Object> objectEvent) {
        if(objectEvent.getContentIfNotHandled() != null) {
            getActivity().finish();
        }
    }
    private void historyList(Event<Object> objectEvent) {
        if(objectEvent.getContentIfNotHandled() != null) {

        }
    }
}
